<template>
  <div class="card" v-if="success" @click="clickCard()">
    <img :src="imgUrl"  alt="图片加载失败" width="100%" height="auto" />
    <div class="card-content">
      <h3 class="card-title">{{ title }}</h3>
      <div class="card-info">
        <span class="card-source">作者{{ uploader }}</span>
        <span class="card-views">收藏： {{ favorite }}</span>
        <span class="card-views">收藏： {{ like }}</span>
        <span class="card-likes">评分： {{ rating }}</span>
      </div>
    </div>
  </div>
</template>

<script lang="ts">
import {cosApi, resourceApi} from "@/api/resourceApi";
import {queryByIdApi} from "@/api/userApi"

import router from "@/router";
import axios from "axios";

export default {
  name: 'EducationCard',
  props: {
    resourceId: {
      type: Number,
      required: true
    },
  },
  data() {
    return {

      imgUrl: '',
      coverName: '',
      resourceType: '',
      title: {
        type: String,
        required: true
      },
      uploader: {
        type: String,
        required: true
      },
      rating: {
        type: Number,
        required: true
      },
      like: {
        type: Number,
        required: true
      },
      favorite: {
        type: Number,
        required: true
      },
      success: false
    }
  },
  methods: {
    async clickCard() {
      router.push({ name: 'player', params: {resourceId: this.resourceId, resourceType: this.resourceType } });
    }
  },
  async created() {
    const resource = (await resourceApi.getbyId(this.resourceId)).data;
    this.title = resource.resourceName;
    this.resourceType = resource.resourceType;
    if (resource.resourceType === 'image') {
      this.imgUrl = (await cosApi.playObject(resource.resourceName)).data;
    }else if (resource.resourceType === 'video') {
      if (resource.coverName!== null) {
        this.imgUrl = (await cosApi.playObject(resource.coverName)).data;
      }else {
        this.imgUrl = (await cosApi.playObject('无封面.png')).data;
      }
    }else if (resource.resourceType === 'text') {
      if (resource.coverName!== null) {
        this.imgUrl = (await cosApi.playObject(resource.coverName)).data;
      }else {
        this.imgUrl = (await cosApi.playObject('无封面.png')).data;
      }
    }
    //待补充,观看数和点赞数
    this.uploader = (await queryByIdApi(resource.userId)).data.username;
    // this.views = 0;
    // this.likes = 0;

    this.success = true;

    const [favoritecount, likecount, ratingcount] = await Promise.all([
      getFavoriteCountApi(this.resourceId),
      getLikeCountApi(this.resourceId),
      getAverageRatingApi(this.resourceId)
    ]);

    this.favorite = favoritecount.data || 0;
    this.like = likecount.data || 0;
    this.rating = ratingcount.data || 0;
  }
};
</script>

<style scoped>
.card {
  display: flex;
  flex-direction: column;
  border: 1px solid #ddd;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  border-radius: 8px;
  overflow: hidden;
  cursor: pointer;
  width: 300px; /* 设置卡片宽度 */
}

.card-img {
  width: 100%;
  height: auto;
  object-fit: cover;
  border-bottom: 1px solid #ddd; /* 图片和文字之间的分隔线 */
}

.card-content {
  padding: 16px;
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}

.card-title {
  font-size: 18px;
  font-weight: bold;
  margin: 0 0 8px;
}

.card-info {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 14px;
  color: #666;
}
</style>